<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-5-6
 * Time: 下午4:41
 */
class Marticle extends CI_Model{
    function __construct(){
        parent::__construct();
    }

    function getArticleAll($title,$column_id,$pageSize,$order='id desc'){
        $where = '1=1 ';
        if(!empty($title)){
            $where .= ' and `title` like "%'.$title.'%" ';
        }
        if(!empty($column_id)){
            $where .= ' and `column_id`='.$column_id;
        }
        $sqlCount = $this->db->query("select * from `tbarticle` where $where");
        $count = $sqlCount->num_rows();

        $sql = "select `id`,`title`,`column_id`,`author`,`create_time`,`is_show`,`sort` from `tbarticle` where {$where} order by $order limit ";

        $query = $this->db->pager($count,$pageSize,$sql);


        //文章
        $article=array();
        foreach($query['data'] as $value){
            $article[] = $value['id'];
        }

        //文章对应的用户
        if(!empty($article)){
            $articleId = implode(',',$article);

            $sqlArticle = "select * from `tbarticle_user` where `article_id` in($articleId) ";
            $queryArticle = $this->db->query($sqlArticle);
            $articleArr = array();
            foreach($queryArticle->result() as $value){
                $articleArr[$value->article_id][] = $value->uid;
            }

        }

        //全部分类
        $column = $this->Mcolumn->loadColumn();

        $columnArr = array();
        foreach($column as $clumnValue){
            $columnArr[$clumnValue->id] = $clumnValue->name;
        }

        //合并文章的分类名和推送用户uid
        foreach($query['data'] as &$value){
            $articleUid = isset($articleArr[$value['id']]) ? implode(',',$articleArr[$value['id']]) :0;
            if(!empty($articleUid)){
                $value['uid'] = $articleUid;
            }else{
                $value['uid'] = '全站文章';
            }

            $value['columnName'] = isset($columnArr[$value['column_id']]) ? $columnArr[$value['column_id']] : '';
            $value['dateTime'] = date("Y-m-d",$value['create_time']);

        }

        return $query;
    }

    function getArticleById($id)
    {

        $sql = "select * from `tbarticle` where `id`=?";
        $query = $this->db->query($sql,array($id));
        $row = $query->row_array();



        //文章对应的用户
        if(!empty($row)){

            $sqlArticle = "select * from `tbarticle_user` where `article_id`=".$row['id'];
            $queryArticle = $this->db->query($sqlArticle);

            $item = array();
            foreach($queryArticle->result() as $value){
                $item[] = $value->uid;
            }
            $row['uids'] = implode(',',$item);
        }
        return $row;
    }

    function getArticleByUid($uid,$p,$pageSize,$field,$order)
    {
        $sqlCount = "select count(*) as count from `tbarticle` as ta left join `tbarticle_user` as tau on ta.id=tau.article_id where tau.`uid` = ?";
        $queryCount = $this->db->query($sqlCount,array($uid));

        $pagestart = ($p-1)*$pageSize;

        $sqlArticle = "select $field from `tbarticle` as ta left join `tbarticle_user` as tau on ta.id=tau.article_id where tau.`uid` = ? order by $order desc limit $pagestart,$pageSize";
        $articleQuery = $this->db->query($sqlArticle,array($uid));

        $data['data'] = $articleQuery->result();
        $data['page'] = $p;
        $data['pagetotle'] = ceil($queryCount/$pageSize);
        $data['count'] = $queryCount['count'];
        return $data;
    }


    function deleteArticle($ids)
    {
        $sql = "DELETE FROM `tbarticle` where `id` in ($ids)";

        $this->db->query($sql);
    }

    function addArticle($title,$content,$column_id,$author,$keyword,$uid,$url,$isShow,$sort)
    {
        $dataTime = time();
        $sql = "INSERT INTO `tbarticle`(`title`,`content`,`column_id`,`author`,`keyword`,`create_time`,`url`,`is_show`,`sort`)VALUES(?,?,?,?,?,$dataTime,?,?,?)";

        $this->db->query($sql,array("$title","$content",$column_id,"$author","$keyword","$url",$isShow,$sort));

        $lastId = $this->db->insert_id();

        if(!empty($lastId) && !empty($uid)){
            $this->Marticleuser->addArticleUid($lastId,$uid);
        }
    }

    function editArticle($id,$title,$content,$column_id,$author,$keyword,$uid,$url,$isShow,$sort)
    {
        if(!empty($uid)){
            $this->Marticleuser->addArticleUid($id,$uid);
        }
        $sql = "UPDATE `tbarticle` set `title` = ?,`content` = ?,`column_id` = ?,`author` = ?,`keyword` = ?,`url`=?,`is_show`=?,`sort`=? WHERE `id` = ?";
        $this->db->query($sql,array("$title","$content",$column_id,"$author","$keyword","$url",$isShow,$sort,$id));
    }

}